/**
 * ${table.comment!} Controller
 * @author jianshengxiong
 */

import React from "react";
import ${entity}Service from "../service/${entity}Service";
import { message } from "antd";
import { bizUtils } from "win-trade-base";

export default class ${entity}ModalController extends React.Component {
    constructor(props) {
        super(props);
        this.${entity?uncap_first}Service = new ${entity}Service(this.props);
    }

    state = {
        isAdd: false,
        showModal: false,
        editData: {}
    };

    // 新增
    handleAdd = () => {
        this.setState({
            isAdd: true,
            showModal: true,
            editData: {}
        });
    };

    // 修改
    handleModify = record => {
        this.setState({
            isAdd: false,
            showModal: true,
            editData: record
        });
    };

    // 修改
    handleModifyById = id => {
        this.${entity?uncap_first}Service.get${entity}Info(id).then((res) => {
            if (res.code !== "200") {
                message.error(res.msg);
                return;
            }
            this.handleModify(res.data);
        });
    };

    // 对话框取消关闭
    handleCancel = () => {
        this.setState({
            showModal: false
        });
    };

    // 对话框确定提交
    handleOk = () => {
        this.$${entity?uncap_first}EditForm.props.form.validateFields((err, values) => {
            if (err) {
                return false;
            }

            values = bizUtils.formatFormData(values);
            if (this.state.isAdd) {
                this.addBond(values);
            } else {
                values = {
                    ...this.state.editData,
                    ...values
                };
                this.updateBond(values);
            }
        });
    };

    get${entity}PageList = () => {
        this.props.loadData();
    }

    add${entity} = values => {
        this.${entity?uncap_first}Service.add${entity}({ ...values }).then(res => {
            if (res.code !== "200") {
                message.error(res.msg);
                return;
            }

            this.setState({
                showModal: false
            });

            this.get${entity}PageList();
        });
    };

    updateBond = values => {
        this.${entity?uncap_first}Service.update${entity}({ ...values }).then(res => {
            if (res.code !== "200") {
                message.error(res.msg);
                return;
            }

            this.setState({
                showModal: false
            });

            this.get${entity}PageList();
        });
    };
}
